﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmAlarmReceiver : FrmMDIChildFormTemplate
    {

        AlarmReceiverBLL bll = new AlarmReceiverBLL();
        private static FrmAlarmReceiver _intance = null;

        private CheckEdit[] checkEdit = null;
        private List<CheckEdit> _checkEditList = new List<CheckEdit>();

        private FrmAlarmReceiver()
        {
            InitializeComponent();
        }


        public static FrmAlarmReceiver GetInstance
        {
            get 
            {
                if(_intance ==null)
                {
                    _intance = new FrmAlarmReceiver();
                }
                return _intance;
            }
        }
        private void FrmAlarmReceiver_Load(object sender, EventArgs e)
        {
            CreateConctrolsOfUser();
        }


        public void CreateConctrolsOfUser()
        {
            groupControl1.Controls.Clear();
            List<users> userList =bll.GetUserList();
            checkEdit = new CheckEdit[userList.Count];

            int left = 20;
            int top = 20;
            bool newRow = false;
            for(int j=0;j<userList.Count;j++)
            {
                checkEdit[j] = new CheckEdit();
                checkEdit[j].CheckedChanged += new EventHandler(FrmAlarmReceiver_CheckedChanged);
                checkEdit[j].Name = "checkEdit" + j;
                checkEdit[j].Width = 115;

                if (left >= groupControl1.Width-50)
                {
                    newRow = true;
                }
                if (newRow)
                {
                    left = 20;
                    top += checkEdit[j].Height;
                    newRow = false;
                }

                checkEdit[j].Left = left;
                checkEdit[j].Top = top;
                left += checkEdit[j].Width;

                checkEdit[j].Text = userList[j].UserName;


                if (userList[j].AlarmReceiver.Count > 0)
                {
                    checkEdit[j].Checked = true;
                }
                else
                {
                    checkEdit[j].Checked = false;
                }
                groupControl1.Controls.Add(checkEdit[j]);
            }

        }

        private void FrmAlarmReceiver_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckEdit)sender).Checked)
            {
                ((CheckEdit)sender).ForeColor = Color.Blue;
            }
            else
            {
                ((CheckEdit)sender).ForeColor = Color.Black;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            btnOK.Enabled = false;
            try
            {
                groupControl1.Refresh();
                foreach (CheckEdit item in groupControl1.Controls)
                {
                    if (item.Checked)
                    {
                        bll.Add(item.Text);
                    }
                    else
                    {
                        bll.Delete(item.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                btnOK.Enabled = true;
                Cursor = Cursors.Default;
            }

            CreateConctrolsOfUser();
            MessageBox.Show("保存成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void FrmAlarmReceiver_FormClosing(object sender, FormClosingEventArgs e)
        {
            _intance = null;

        }


    }
}
